/**
 * 
 */
package runner;

import image.Image;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

/**
 * @author nquangtrung
 *
 */
public class ImageDisplayer extends JPanel {
	public static final String TAG = "ImageDisplayer";
	private Image image;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	public ImageDisplayer(Image image) {
		this.setImage(image);
	}
	
	@Override
	public void paint(Graphics p) {
		setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
		
		Graphics2D g2 = (Graphics2D) p;
		for (int y = 0; y < image.getHeight(); y++) {
			for (int x = 0; x < image.getWidth(); x++) {
				int r = image.getRed(x, y);
				int g = image.getGreen(x, y);
				int b = image.getBlue(x, y);
				g2.setColor(new Color(r, g, b));
				// Log.d(TAG, "Red: " + r);
				g2.drawRect(x, image.getHeight() - y - 1, 0, 0);
			}
		}
	}

	/**
	 * @return the image
	 */
	public Image getImage() {
		return image;
	}

	/**
	 * @param image the image to set
	 */
	public void setImage(Image image) {
		this.image = image;
		this.setSize(image.getWidth(), image.getHeight());
		this.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
		this.setMinimumSize(new Dimension(image.getWidth(), image.getHeight()));
		this.setMaximumSize(new Dimension(image.getWidth(), image.getHeight()));
		this.repaint();
	}

}
